; demo.asm
; written for the SC-2 computer
; program gets keyboard input from user and prints out that input
;

BEGIN		.ORIG	x3000		;
		LDA	$R1, NAMEINPUT	; use $R1 as a refernce address
		LDI	$R3, #1
		JSR	$R1		; go get the name
; print it out
		LDA	$RIP0, NAME
PRINTIT		LDB	$R2&
		MOV	$R5, $R2
		SUB	$R5, $R3
		BRN	FINISH
		OUT	$R2, VIDDATA
		BR	PRINTIT
FINISH		HALT

; subroutine to print prompt and get key inputs
NAMEINPUT	LDA	$RIP0, PROMPT
		LDI	$R3, #1
PRINTPROMPT	LDB	$R2&			; indexed-mode retrieve characters
		MOV	$R5, $R2
		SUB	$R5, $R1			; see if char is NULL = 0
		BRN	GETDATA
		OUT	$R2, VIDDATA
		BR	PRINTPROMPT
GETDATA		LDA	$R1, NEWLINE
		LDB	$R2, $R1
		OUT	$R2, VIDDATA
		ADD	$R1, $R3
		LDB	$R2, $R1
		OUT	$R2, VIDDATA
		LDA	$RIP1, NAME
		LDI	$R3, x000A
GETKEY		IN	$R2, KBDDATA
		OUT	$R2, VIDDATA		; echo char
		MOV	$R5, $R2
		SUB	$R5, $R3		; test for CR
		BRZ	TERMINATE
		STB	$R2&			; store and increment
		BR 	GETKEY
TERMINATE	LDI	$R2, #0
		STB	$R2, $RIP1		; NULL terminate string
		
LEAVE		RET
		
VIDDATA		.DEF	x32
KBDDATA		.DEF	x3A

PROMPT		.STRING	"Enter your name: "
NEWLINE		.DW	x0A0D
NAME		.RW	#32
		.END

